package org.idea.qiyu.framework.apm.agent.collector.controller;

import com.alibaba.fastjson.JSON;
import org.idea.qiyu.common.dto.AgentCollectorDataDTO;
import org.idea.qiyu.framework.apm.agent.collector.service.IAgentCollectorService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author linhao
 * @Date created in 7:27 下午 2021/9/9
 */
@RestController
@RequestMapping(value = "/agent")
public class AgentCollectorController {

    @Resource
    private IAgentCollectorService iAgentCollectorService;

    @PostMapping(value = "/report")
    public boolean insertAgentCollectData(String jsonInput) {
        if (StringUtils.isEmpty(jsonInput)) {
            return false;
        }
        System.out.println("input is :" + jsonInput);
        AgentCollectorDataDTO agentCollectorDataDTO = JSON.parseObject(jsonInput, AgentCollectorDataDTO.class);
        if (agentCollectorDataDTO != null) {
            return iAgentCollectorService.reportData(agentCollectorDataDTO);
        }
        return false;
    }

    @GetMapping(value = "/get-all")
    public Map<String, Object> getAll() {
        return iAgentCollectorService.getAllData();
    }
}
